import requests

zhaopBse = requests.session()

# 北京 530 深圳765  广州763  上海538 长沙749 杭州653
city_id = [530,765,763,538,749,653]


headers = {
    'Accept':'application/json, text/plain, */*',
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.131 Safari/537.36',
    'Referer':"https://sou.zhaopin.com/?p=2&jl=653&sf=0&st=0&kw=%E5%A4%A7%E6%95%B0%E6%8D%AE%E5%B7%A5%E7%A8%8B%E5%B8%88&kt=3",
}
city_name = {"530":"北京","765":"深圳","538":"上海","749":"长沙","763":"广州","653":"杭州"}
for i in city_id:
    # 文武需要的id,每次切换城市都会重新统计
    bb_id = 0
    # start 爬取数量 , PageSize每页数据至多90/页 cityId:城市ID
    # data = {
    #     'cityId':i,
    #     'start':400,
    #     'pageSize':90,
    #     'workExperience':-1,
    #     'companyType':-1,
    #     'kw':'%E5%A4%A7%E6%95%B0%E6%8D%AE%E5%B7%A5%E7%A8%8B%E5%B8%88&kt=3',
    #     'x-zp-page-request-id':''
    # }
    url = "https://fe-api.zhaopin.com/c/i/sou?start=90&pageSize=90&cityId="+str(i)+"&workExperience=-1&education=-1&companyType=-1&employmentType=-1&jobWelfareTag=-1&kw=%E5%A4%A7%E6%95%B0%E6%8D%AE%E5%B7%A5%E7%A8%8B%E5%B8%88&kt=3&_v=0.74770893&x-zp-page-request-id=0b536d41a99d4e1db76a482484661777-1557402769560-275288"
    response_zhaopin = zhaopBse.get(headers=headers,url=url).json()
    infor_datas = response_zhaopin['data']['results']
    for infor_data in infor_datas:
        # 职位名称
        job_name = infor_data['jobName']
        # 薪水
        salary = infor_data['salary']
        # 福利待遇
        welfare = infor_data['welfare']
        # 工作年限
        workingExp = infor_data['workingExp']['name']
        # 学历要求
        edu = infor_data['eduLevel']['name']
        # 全职or兼职
        emplType = infor_data['emplType']
        # 发布时间
        createDate = infor_data['createDate']
        # 结束时间
        endDate = infor_data['endDate']
        # 公司名称
        company_name = infor_data['company']['name']
        # 公司规模
        company_size = infor_data['company']['size']['name']
        # 公司类型
        company_type = infor_data['company']['type']['name']
        # 公司主页
        company_url = infor_data['company']['url']
        # 详情地址
        position_url = infor_data['positionURL']
        bb_id = bb_id + 1
        data_tmp = {
            "bb_id":bb_id,
            "job_name":job_name,
            "salary":salary,
            "welfare":welfare,
            "workage":workingExp,
            "edu":edu,
            "emplType":emplType,
            "createDate":createDate,
            "endDate":endDate,
            "company_name":company_name,
            "company_size":company_size,
            "company_type":company_type,
            "company_url":company_url,
            "position_url":position_url
        }
        name_tmp = city_name[str(i)]
        with open(name_tmp+".json","a+",encoding='utf-8',newline='') as fp:
            fp.write(str(data_tmp)+'\n')
